package com.pristine.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.pristine.dao.IPatientDao;
import com.pristine.domain.PatientEntity;
import com.pristine.service.wsdl.IPatientService;
import com.pristine.service.wsdl.PatientVO;
import com.pristine.util.EntityToVOConverter;
import com.pristine.util.VOToEntityConverter;


@Service("patientService")
@Transactional
public class PatientServiceImpl implements IPatientService{

	@Autowired
	private IPatientDao patientDao;
	
	@Override
	public Integer createPatient(PatientVO patientVO){
		PatientEntity patient = VOToEntityConverter.convert(patientVO);
		patientDao.add(patient);
		return patient.getUhid();
	}
	
	@Override
	public Integer updatePatient(PatientVO patientVO){
		PatientEntity patient = VOToEntityConverter.convert(patientVO);
		patientDao.update(patient);
		return patient.getUhid();
	}
	
	@Override
	public boolean deletePatient(Integer id){
		try{
			PatientEntity entity = patientDao.find(id);
			patientDao.remove(entity);
			return true;
		}catch(Exception e){
			return false;
		}
	}
	
	@Override
	public PatientVO getPatientById(Integer id){
		return EntityToVOConverter.convert(patientDao.find(id));
	}
	
	@Override
	public List<PatientVO> getAllPatient() {
		
		List<PatientEntity> list = patientDao.list();
		List<PatientVO> voList= new ArrayList<PatientVO>();
		if(CollectionUtils.isNotEmpty(list)){
			for(PatientEntity entity:list){
				voList.add(EntityToVOConverter.convert(entity));
			}
		}
		return voList;
	}
	
}
